//
//  BIZPolicyAnalysisWebCell.m
//  biz
//
//  Created by LXL on 2017/7/8.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZPolicyAnalysisWebCell.h"
@interface BIZPolicyAnalysisWebCell()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *webViewHeight;
@property (nonatomic, assign) CGFloat expectdHeight;

@end

@implementation BIZPolicyAnalysisWebCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.webView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    self.webView.multipleTouchEnabled = NO;
    self.webView.autoresizesSubviews = YES;
    self.webView.opaque = NO;
    self.webView.scrollView.scrollEnabled = NO;
    self.webView.scrollView.showsHorizontalScrollIndicator = NO;
    self.webView.scrollView.showsVerticalScrollIndicator = NO;
    self.webView.scrollView.bounces = NO;
    self.webView.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.webView.delegate = self;
    
    _expectdHeight = 0;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setHtml:(NSString *)html {
    if ([_html isEqualToString:html])  return;
    
    _html = [html copy];
    if (!self.webView.isLoading && _html != nil && ![_html isEqualToString:@""]) {
        NSString *HTML = [_html stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
        [self.webView loadHTMLString:HTML baseURL:nil];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if(!webView.isLoading){
        
        CGFloat height = [webView sizeThatFits:CGSizeZero].height + 10;
        if (height != _webViewHeight.constant) {
            
            _webViewHeight.constant = height;
            _expectdHeight = height;
                        DeLog(@"height %f", height);
            if (self.delegate && [self.delegate respondsToSelector:@selector(policyAnalysisWebCell:heightDidChange:)]) {
                [self.delegate policyAnalysisWebCell:self heightDidChange:height];
            }
        }
    }
}


- (void)stopLoading {
    if ([self.webView isLoading]) {
        [self.webView stopLoading];
    }
    
    [self cleanCacheAndCookie];
}

- (void)cleanCacheAndCookie {
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
    
}


@end
